package com.sgb.kjwl.view.widget;

import android.content.Context;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.ItemTouchHelper;

import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.sgb.kjwl.R;
import com.swgk.core.view.entity.ChannelEntity;
import com.swgk.core.view.adapter.TitleAdapter;
import com.swgk.core.customview.ItemDragHelperCallback;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import java.util.ArrayList;
import java.util.List;

public class TitlePopWindow extends BasePopWindow {

    private RecyclerView recyclerView;
    private TextView okView;
    private TextView resetView;
    private TitleAdapter titleAdapter;
    private List<ChannelEntity> channelEntityList;
    private String no = "";
    private String name = "";

    public TitlePopWindow(Context context,List<ChannelEntity> channelEntityList) {
        super(context);
        this.channelEntityList = channelEntityList;
        titleAdapter.setData(channelEntityList);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_title;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        recyclerView = holder.getPop().findViewById(R.id.rlv_list);
        okView = holder.getPop().findViewById(R.id.ok_view);
        resetView = holder.getPop().findViewById(R.id.reset_view);
        okView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissMiss();
                if (onResultClick != null) {
                    onResultClick.result(no, name,null);
                }
            }
        });

        channelEntityList = new ArrayList<>();
        GridLayoutManager manager = new GridLayoutManager(mContext, 3);
        recyclerView.setLayoutManager(manager);
        ItemDragHelperCallback callback = new ItemDragHelperCallback();
        final ItemTouchHelper helper = new ItemTouchHelper(callback);
        helper.attachToRecyclerView(recyclerView);
        titleAdapter = new TitleAdapter(mContext, helper, channelEntityList, channelEntityList);

        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                int viewType = titleAdapter.getItemViewType(position);
                return viewType == TitleAdapter.TYPE_MY || viewType == TitleAdapter.TYPE_OTHER ? 1 : 3;
            }
        });
        recyclerView.setAdapter(titleAdapter);

        titleAdapter.setOnMyChannelItemClickListener(new TitleAdapter.OnMyChannelItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                Toast.makeText(mContext, channelEntityList.get(position).getName(), Toast.LENGTH_SHORT).show();
            }
        });
        titleAdapter.setOnFinishClick(new TitleAdapter.OnFinishClick() {
            @Override
            public void onFinish(List<ChannelEntity> editList) {
                dissMiss();
                if (onResultClick != null) {
                    onResultClick.result(editList, null,null);
                }
            }
        });
    }

    /*public void setData(List<String> areaEntityList) {
        this.areaEntityList = areaEntityList;
        titleAdapter.notifyDataSetChanged();
    }*/

    public void setTitle(String title){
        titleAdapter.setTitle(title);
    }

    public void setInitialization() {
        titleAdapter.setInitializationShow();
    }
}
